import { call, put, takeEvery, all, fork } from 'redux-saga/effects';
import request from '@/utils/request';
import {
  SAVE_LOGIN_INFO,
  FETCH_REFRESH_TOKEN,
  GET_QINIUTOKEN,
  SAVE_TENANT_INFO,
  FETCH_TENANT_TOKEN,
} from '@/reducers/common';




//刷新token接口
function* refreshToken() {
  yield takeEvery(FETCH_REFRESH_TOKEN, function* ({ payload, resolve, reject }: any) {
    try {
      const response: GobalsResponse = yield call(request.post, '/mgt/user/refresh-token', payload);
      yield put({ type: SAVE_LOGIN_INFO, data: response.data });
      resolve && resolve(response.data);
    } catch (error) {
      reject && reject(error)
    }
  })
}
//刷新租户token接口
function* refreshTenantToken() {
  yield takeEvery(FETCH_TENANT_TOKEN, function* ({ payload, resolve, reject }: any) {
    try {
      const response: GobalsResponse = yield call(request.post, '/mgt/user/refresh-token', payload);
      yield put({ type: SAVE_TENANT_INFO, data: response.data });
      resolve && resolve(response.data);
    } catch (error) {
      reject && reject(error)
    }
  })
}

//获取七牛token
function* getQiniuUploadToken() {
  yield takeEvery(GET_QINIUTOKEN, function* ({ payload, resolve, reject }: any) {
    try {
      const response: GobalsResponse = yield call(request.post, '/registration/user/qiniu_token', payload);
      resolve && resolve(response.data);
    } catch (error) {
      reject && reject(error)
    }
  })
}

export default function* commonFLow() {
  yield all([
    fork(refreshTenantToken),
    fork(refreshToken),
    fork(getQiniuUploadToken),
  ])
}
